package org.example.demo.service.impl;

import org.example.demo.entity.User;
import org.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: Fsk App
 * @Copyright: com.fsk
 * @Version: 1.0.0
 * @Author: chenming2
 * @UpdateTime: 3/2/2022
 * @Describe: XXXXXXX.
 */
@Service
public class UserServiceImpl implements UserService {
    private final Map<Integer, User> users = new HashMap<>();

    @Override
    public Flux<User> selectAll() {
        return Flux.fromIterable(users.values());
    }

    @Override
    public Mono<User> getUserById(Integer id) {
        return Mono.justOrEmpty(users.get(id));
    }

    @Override
    public Mono<Void> add(Mono<User> user) {
        return user.doOnNext(user1 -> {
            int i = users.size() + 1;
            users.put(i, user1);
        }).thenEmpty(Mono.empty());
        // 结束数据流
    }
}
